import 'package:flutter/material.dart';
import 'Header2.dart';
import 'Header1.dart';
import 'models/FuLi.dart';
import 'models/Girl.dart';
import 'package:dio/dio.dart';

class PageFirst extends StatefulWidget {
  @override
  _PageFirstState createState() => _PageFirstState();
}

class _PageFirstState extends State<PageFirst>
    with AutomaticKeepAliveClientMixin {
  final _suggestions = <String>[];
  final _biggerFont = const TextStyle(fontSize: 19.0);
  List<Girl> _girls = new List();
  int curPage = 1;
  ScrollController _controller = new ScrollController();

  @override
  void initState() {
    ///初始化时创建控制器
    super.initState();
    getFuli();
    _controller.addListener(() {
      var maxScroll = _controller.position.maxScrollExtent;
      var pixels = _controller.position.pixels;
      if (maxScroll == pixels && _girls.length > 10) {
        curPage++;
        getFuli();
      }
    });
  }

  void getFuli() async {
    try {
      Dio dio = new Dio();
      Options options = new Options(
          baseUrl: "http://gank.io/api/data/福利",
          connectTimeout: 5000,
          receiveTimeout: 5000);
      dio.options = options;
      Future future = dio.get("/10/$curPage");
      future.then((data) {
        print(data);
        FuLi fuLi = new FuLi.fromJson(data.data);
        if (curPage == 1) {
          _girls.clear();
          _girls = fuLi.results;
        } else {
          _girls.addAll(fuLi.results);
        }
        setState(() {});
        print(fuLi.results);
      }).catchError((error) {
        print(error);
      });
//    Response response;
//    response = await Dio().get("http://gank.io/api/data/福利/10/1");
//    return print(response);
    } catch (e) {
      return print(e);
    }
  }

  Future<Null> _refreshHelper() {
    curPage = 1;
    getFuli();
    return null;
  }

  Widget _buildSuggestions() {
    return RefreshIndicator(
      onRefresh: _refreshHelper,
      child: new ListView.separated(
          controller: _controller,
          itemBuilder: (context, i) {
            return Header2(
              girl: _girls.isNotEmpty ? _girls[i] : "",
            );
          },
          separatorBuilder: (context, i) {
            return i == 0 ? Header1() : Header2();
          },
          itemCount: _girls.length),
    );
//    return new ListView.builder(
//        itemBuilder: (context, i) {
//          return Header2(
//            girl: _girls.isNotEmpty ? _girls[i] : "",
//          );
//        }
////        separatorBuilder: (context,i){
////          return i==0?Header1():Header2();
////        }
//        ,
//        itemCount: _girls.length);
//    return new ListView.builder(
////        padding: const EdgeInsets.all(16.0),
//        itemBuilder: (context, i) {
//          if (i.isOdd) return new Divider();
//          final index = i ~/ 2;
//          if (index >= _suggestions.length) {
//            _suggestions.addAll(generateWordPairs());
//          }
//          return _buildRow(_suggestions[index]);
//        });
  }

  @override
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    super.build(context);
    return _buildSuggestions();
  }
}
